<?php

function accessibility_filter($courseid, $text) {
	global $CFG;
	//if(!has_capability('mod/accessibility:access_overview', 'module')) {
	//	return $text;
	//}
	if(strip_tags(trim($text)) == trim($text)) {
		return $text;
	}
	return accessibility_filter_check_text($text, $courseid);
}

function accessibility_filter_check_text($text, $course_id) {
	global $CFG;
	$results = get_record('accessibility_test_results', 'hash', md5($text));
	if(is_object($results)) {
		return '<div id="accessibility_'. md5($text) .'">'. $text . '</div>'. 
			accessibility_filter_add_button($results,  $text, true);
	}
	require_once('quail/quail.php');
	$quail = new quail($text, $CFG->filter_accessibility_guideline, 'string', 'array');
	$quail->setOption('cms_mode', TRUE);
	$quail->runCheck();
	$report = $quail->getReport(array('display_level' => $CFG->filter_accessibility_display_level));
	$report = accessibility_filter_cleanup_report($report);
	$insert = new stdClass();
	$insert->hash = md5($text);
	$insert->course_id = $course_id;
	$insert->number_severe = $report[QUAIL_TEST_SEVERE]['total'];
	$insert->number_moderate = $report[QUAIL_TEST_MODERATE]['total'];
	$insert->number_suggestion = $report[QUAIL_TEST_SUGGESTION]['total'];
	insert_record('accessibility_test_results', $insert, true);
	return '<div id="accessibility_'. $insert->hash .'">'. $text . '</div>'. 
			accessibility_filter_add_button($report, $text);
}

function accessibility_filter_cleanup_report($report) {
	//Start with an empty result set which will have empty error totals
	$result = array(QUAIL_TEST_SEVERE => array('total' => 0),
					QUAIL_TEST_MODERATE => array('total' => 0),
					QUAIL_TEST_SUGGESTION => array('total' => 0),
	);
	foreach($report as $testname => $test) {
		if(isset($test['problems']) && count($test['problems']) > 0) {
			//only tests where there were no issues to report
			$result[$test['severity']][$testname] = $test;
			if(isset($test['problems']['pass']) && $test['problems']['pass'] === false) {
				$result[$test['severity']]['total']++;
			}
			else {
				$result[$test['severity']]['total'] += count($test['problems']);
			}
		}
	}
	return $result;
}

function accessibility_filter_add_button($results, $text, $db = false) {
	global $CFG;
	if($db) {
		$totals = array(
			'severe' => $results->number_severe,
			'moderate' => $results->number_moderate,
			'suggestion' => $results->number_suggestion,
		);
	}
	else {
		$totals = array(
			'severe' => $results[QUAIL_TEST_SEVERE]['total'],
			'moderate' => $results[QUAIL_TEST_MODERATE]['total'],
			'suggestion' => $results[QUAIL_TEST_SUGGESTION]['total'],
		);
	}
	return '<div class="accessibility-overview"><h5><img src="'. $CFG->wwwroot .
			'/filter/accessibility/images/accessibility.gif" alt="">'. 
				get_string('accessibilityoverview', 'filter_accessibility') .':</h5> '.
			'<form method="post" action="'. $CFG->wwwroot .'/filter/accessibility/overview.php">'.
			'<input type="hidden" name="text" value="'. htmlspecialchars($text) .'">'.
			'<input type="submit" value="View Overview"/></form>'.
			'<form method="post" action="'. $CFG->wwwroot .'/filter/accessibility/highlight.php">'.
			'<input type="hidden" name="text" value="'. htmlspecialchars($text) .'">'.
			'<input type="submit" value="Highlight Problems"/></form>'.
			'<span class="severe"><img src="'. $CFG->wwwroot .
				'/filter/accessibility/images/severe.png" alt="">'. 
				get_string('severe', 'filter_accessibility') .': '. 
					$totals['severe'] . '</span>'.
			'<span class="moderate"><img src="'. $CFG->wwwroot .
				'/filter/accessibility/images/moderate.png" alt="">'. 
				get_string('moderate', 'filter_accessibility') .': '. $totals['moderate'] . '</span>'.
		'<span class="suggestion"><img src="'. $CFG->wwwroot .
				'/filter/accessibility/images/suggestion.png" alt="">'. 
				get_string('suggestion', 'filter_accessibility') .': '. $totals['suggestion'] . '</span></div>';
}